<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理案例练习</title>
    <script src="./js/vue.js"></script>
    <style>
        .gard {
            text-align: center;
            width: 500px;
            margin: 0 auto;
        }

        .header {
            background-color: orange;
            text-align: center;
            padding: 2px 0 2px 0;
        }

        .gard table {
            width: 100%;
            border-collapse: collapse;
        }

        .gard th td {
            text-align: center;
            padding: 10px;
            height: 35px;
            line-height: 35px;
            border: 1px dashed orange;
        }

        .gard th {
            background-color: orange;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="gard">
            <h1>图书管理系统</h1>
            <div class="header">
                编号:
                <input type="text" name="" value="" v-model='uid'>
                名称：
                <input type="text" name="" value="" v-model='uname'>
                <button v-on:click='submit'>提交</button>
            </div>
            <table>
                <thead>
                    <tr class="tabel_header">
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>

                <tbody>
                    <tr :key='item.id' v-for='item in books'>
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.data}}</td>
                        <td> <a href="#" v-on:click.prevent='toEdit(item.id)'>修改</a>
                            <span> | </span>
                            <a href="#" v-on:click.prevent='toDelete(item.id)'>删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                books: [
                    {
                        id: 1,
                        name: "三国演义",
                        data: '---------'
                    },
                    {
                        id: 2,
                        name: "水浒传",
                        data: '---------'
                    },
                    {
                        id: 3,
                        name: "红楼梦",
                        data: '---------'
                    },
                    {
                        id: 4,
                        name: "西游记",
                        data: '---------'
                    }
                ],
                index: 1,
                uid: '',
                uname: ''
            },
            methods: {
                submit: function () {
                    // 提交 向数组中添加一条数据
                    var book = {};
                    book.id = this.uid;
                    book.name = this.uname;
                    book.data = '----------';
                    this.books.push(book);
                    // 清空输入框数据
                    this.uname = '';
                    this.uid = '';
                },
                // 修改方法
                toEdit: function (id) {
                    console.log('修改项--- ' + id);
                },
                // 删除方法
                toDelete: function (id) {
                    console.log('删除项----' + id);
                }
            }
        })
    </script>
</body>

</html>